package com.kungfuhacking.wristbandpro.location.presenter;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.base.view.HomeActivity;
import com.kungfuhacking.wristbandpro.location.view.ISNCodeView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * 绑定手环
 * Created by Aaron on 17/5/9.
 */

public class ISNCodePresenter implements BasePresenter {

    private ISNCodeView iSNCodeView;

    public ISNCodePresenter(ISNCodeView isnCodeView) {
        this.iSNCodeView = isnCodeView;
    }

    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }

    /**
     * 通过IEMI码绑定手环
     *
     * @param imei
     */
    public void bindByIEMI(String imei) {

        JSONObject j = new JSONObject();

        j.put("id", iSNCodeView.getSp().getString(Constant.CURRENTOLDID, ""));
        j.put("token", iSNCodeView.getToken());
        j.put("sn", imei);

        RetrofitHttpUtil.getInstance().addWatch(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                iSNCodeView.hideLoading();
                bindSuccess();
            }

            @Override
            public void onHandleError(String message) {
                iSNCodeView.hideLoading();
                iSNCodeView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iSNCodeView.showErr(message);
                iSNCodeView.hideLoading();
                iSNCodeView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {

                compositeDisposable.add(d);
                iSNCodeView.showLoading("绑定中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    private void bindSuccess() {
        iSNCodeView.showErr("绑定成功");
        //通过IMEI码绑定的是管理员权限
        iSNCodeView.getSp().edit().putInt(Constant.LEVEL, Constant.POWER_ADMIN).apply();
        iSNCodeView.nextActivity(HomeActivity.class);
    }


    public void bindByScan(String imei) {
        JSONObject j = new JSONObject();

        j.put("id", iSNCodeView.getSp().getString(Constant.CURRENTOLDID, ""));
        j.put("token", iSNCodeView.getToken());
        j.put("sn", imei);

        RetrofitHttpUtil.getInstance().bindYellow(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                iSNCodeView.hideLoading();
                bindSuccess();
            }

            @Override
            public void onHandleError(String message) {
                iSNCodeView.hideLoading();
                iSNCodeView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iSNCodeView.showErr(message);
                iSNCodeView.hideLoading();
                iSNCodeView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iSNCodeView.showLoading("绑定中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

}
